package com.wf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wf.domain.RoleMenu;
import com.wf.mapper.RoleMenuMapper;
import com.wf.service.RoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
* @author mpk16
* @description 针对表【role_menu】的数据库操作Service实现
* @createDate 2024-08-03 13:39:49
*/
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu>
    implements RoleMenuService{
    @Autowired
    RoleMenuMapper roleMenuMapper;
    @Override
    public boolean editRole(Map<String, Object> role) {
        boolean flag = false;
        System.out.println("role = " + role);
        List<Map<String,Object>> menus;
        if (role.get("menus")!=null){
            menus = (List<Map<String,Object>>) role.get("menus");
//            menus.remove(0);
            System.out.println("menus = " + menus);
            //根据role_id删除role_menu表中数据
            QueryWrapper<RoleMenu> wrapper = new QueryWrapper<>();
            System.out.println("role.get(\"roleId\") = " + role.get("roleId"));
            wrapper.eq("role_id",role.get("roleId"));
            roleMenuMapper.delete(wrapper);
            int count = 0;
            for (int j = 0; j < menus.size(); j++) {
                System.out.println("menus.get(j) = " + menus.get(j));
                if (menus.get(j).get("menuId")!=null){
                    RoleMenu roleMenu = new RoleMenu();
                    roleMenu.setRoleId(Long.valueOf(role.get("roleId")+""));
                    roleMenu.setMenuId(Long.valueOf(menus.get(j).get("menuId")+""));
                    count += roleMenuMapper.insert(roleMenu);
                }
                if (!(menus.get(j).get("childrenList") =="")&&!(menus.get(j).get("childrenList") =="[]")&&!(menus.get(j).get("childrenList") ==null)){
                    List<Map<String,Object>> list= (List<Map<String, Object>>) menus.get(j).get("childrenList");
                    for (int i = 0; i < list.size(); i++) {
                        RoleMenu rm = new RoleMenu();
                        rm.setRoleId(Long.valueOf(role.get("roleId")+""));
                        System.out.println("list.get(i).get(\"menuId\") = " + list.get(i).get("menuId"));
                        rm.setMenuId(Long.valueOf(list.get(i).get("menuId")+""));
                        count += roleMenuMapper.insert(rm);
                    }
                }
            }
            if (count>0){
                flag = true;
            }
        }
        return flag;
    }
}




